package com.study.springmvc.core;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;

public class DetachedCriteriaFactory {
    public final static String VO_PATH = "com.study.springmvc.biz.module";
    public final static String DO_PATH = "com.study.springmvc.dal.dataobject";

    public static Log          log     = LogFactory.getLog(DetachedCriteriaFactory.class);

    public static DetachedCriteria InitDetachedCriteriaByVO(Class ViewObjectClass) {
        String DOClassName = ViewObjectClass.getName().replaceAll(VO_PATH, DO_PATH).substring(0,
            ViewObjectClass.getName().lastIndexOf("VO"))
                             + "DO";
        try {
            Class clazz = Class.forName(DOClassName);
            return DetachedCriteria.forClass(clazz);
        } catch (Exception e) {
            if (log.isInfoEnabled()) {
                log.info("get criteria fail,please check vo!");
            }
            return null;
        }
    }
}
